package org.jeecg.cn.edu.scau.vm.image.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.jeecg.cn.edu.scau.vm.image.domain.VmImage;
import org.jeecg.cn.edu.scau.vm.image.enums.VmImageStatusEnum;
import org.jeecg.cn.edu.scau.vm.image.service.VmImageService;
import org.jeecg.cn.edu.scau.vm.image.vo.VmImageListVo;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;

/**
 * 镜像控制器
 */
@RequestMapping("/vm/vmImage")
@RestController
public class VmImageController {
    @Resource
    private VmImageService vmImageService;

    /**
     * 通用分页列表查询
     *
     * @param vmImage  镜像数据查询实例
     * @param pageNo   页号
     * @param pageSize 分页大小
     * @param req      请求体
     * @return 返回数据
     */
    //@AutoLog(value = "vm_image-分页列表查询")
    @ApiOperation(value = "vm_image-分页列表查询", notes = "vm_image-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<VmImageListVo>> queryPageList(VmImage vmImage,
                                                      @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                      @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                      HttpServletRequest req) {
        QueryWrapper<VmImage> queryWrapper = QueryGenerator.initQueryWrapper(vmImage, req.getParameterMap());
        Page<VmImage> page = new Page<>(pageNo, pageSize);
        IPage<VmImageListVo> pageList = vmImageService.pageList(page, queryWrapper);
        return Result.OK(pageList);
    }

    @ApiOperation(value = "vm_image-查询个人镜像", notes = "vm_image-查询个人镜像")
    @GetMapping(value = "/listMy")
    public Result<IPage<VmImageListVo>> queryPageListMy(VmImage vmImage,
                                                        @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                        @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                        HttpServletRequest req) {
        QueryWrapper<VmImage> queryWrapper = QueryGenerator.initQueryWrapper(vmImage, req.getParameterMap());
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        queryWrapper.eq("create_by", loginUser.getId());
        Page<VmImage> page = new Page<>(pageNo, pageSize);
        IPage<VmImageListVo> pageList = vmImageService.pageList(page, queryWrapper);
        return Result.OK(pageList);
    }


    /**
     * 查询待审核镜像
     *
     * @param vmImage  镜像数据查询实例
     * @param pageNo   页号
     * @param pageSize 分页大小
     * @param req      请求体
     * @return 返回数据
     */
    @ApiOperation(value = "vm_image-分页列表查询", notes = "vm_image-分页列表查询")
    @GetMapping(value = "/listToReview")
    public Result<IPage<VmImageListVo>> queryPageListToReview(VmImage vmImage,
                                                              @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                              @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                              HttpServletRequest req) {
        QueryWrapper<VmImage> queryWrapper = QueryGenerator.initQueryWrapper(vmImage, req.getParameterMap());
        Page<VmImage> page = new Page<>(pageNo, pageSize);
        // 添加待审核条件
        queryWrapper.eq("status", VmImageStatusEnum.IMAGE_WAITING.getCode());
        IPage<VmImageListVo> pageList = vmImageService.pageList(page, queryWrapper);
        return Result.OK(pageList);
    }


    /**
     * 查询审核通过镜像
     *
     * @param vmImage  镜像数据查询实例
     * @param pageNo   页号
     * @param pageSize 分页大小
     * @param req      请求体
     * @return 返回数据
     */
    @ApiOperation(value = "vm_image-分页列表查询", notes = "vm_image-分页列表查询")
    @GetMapping(value = "/listReviewPass")
    public Result<IPage<VmImageListVo>> queryPageReviewPass(VmImage vmImage,
                                                            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                            HttpServletRequest req) {
        QueryWrapper<VmImage> queryWrapper = QueryGenerator.initQueryWrapper(vmImage, req.getParameterMap());
        Page<VmImage> page = new Page<>(pageNo, pageSize);
        // 添加待审核条件
        queryWrapper.eq("status", VmImageStatusEnum.IMAGE_PASSED.getCode());
        IPage<VmImageListVo> pageList = vmImageService.pageList(page, queryWrapper);
        return Result.OK(pageList);
    }


    /**
     * 录入镜像
     *
     * @param vmImage 镜像相关数据
     * @return 返回数据
     */
    @AutoLog(value = "vm_image-添加")
    @ApiOperation(value = "vm_image-添加", notes = "vm_image-添加")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody VmImage vmImage) {
        vmImageService.saveImage(vmImage);
        return Result.OK("添加成功！");
    }


    /**
     * 编辑镜像
     *
     * @param vmImage 镜像实体类
     * @return 返回提示信息
     */
    @AutoLog(value = "vm_image-编辑")
    @ApiOperation(value = "vm_image-编辑", notes = "vm_image-编辑")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody VmImage vmImage) {
        vmImageService.updateById(vmImage);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id 镜像ID
     * @return 返回提交审核通过
     */
    @AutoLog(value = "vm_image-通过id删除")
    @ApiOperation(value = "vm_image-通过id删除", notes = "vm_image-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
        vmImageService.removeById(id);
        return Result.OK("删除成功!");
    }

    @AutoLog(value = "vm_image提交审核")
    @ApiOperation(value = "vm_image-提交审核", notes = "vm_image-提交审核")
    @DeleteMapping(value = "/submitReview")
    public Result<String> submitReview(@RequestParam(name = "id", required = true) String id) {
        vmImageService.submitReview(id);
        return Result.OK("提交审核成功!");
    }

    /**
     * 审核通过
     *
     * @param vmImage 审核已通过镜像
     * @return 返回信息
     */
    @AutoLog(value = "vm_image-审核通过")
    @ApiOperation(value = "vm_image-审核通过", notes = "vm_image-审核通过")
    @RequestMapping(value = "/reviewPass", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> reviewPass(@RequestBody VmImage vmImage) {
        vmImageService.reviewPass(vmImage);
        return Result.OK("审批完成!");
    }

    /**
     * 审核不通过
     *
     * @param vmImage 审核已通过镜像
     * @return 返回信息
     */
    @AutoLog(value = "vm_image-审核通过")
    @ApiOperation(value = "vm_image-审核通过", notes = "vm_image-审核通过")
    @RequestMapping(value = "/reviewReject", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> reviewReject(@RequestBody VmImage vmImage) {
        vmImageService.reviewReject(vmImage);
        return Result.OK("审批完成!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "vm_image-批量删除")
    @ApiOperation(value = "vm_image-批量删除", notes = "vm_image-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.vmImageService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "vm_image-通过id查询")
    @ApiOperation(value = "vm_image-通过id查询", notes = "vm_image-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<VmImage> queryById(@RequestParam(name = "id", required = true) String id) {
        VmImage vmImage = vmImageService.getById(id);
        if (vmImage == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(vmImage);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param vmImage
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, VmImage vmImage) {
        return null;
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return null;
    }

}
